#include "encoder.h"

#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"

#include "buzzer.h"
#include "oled_task.h"

encoder_data_t encoder_data;
extern oled_task_data_t oled_task_data;

static void main_faction_handle(void);
static void select_faction_handle0(void);
static void wifi_faction_handle0(void);
static void server_faction_handle0(void);
static void b_faction_handle0(void);

static void select_faction_handle1(void);    //功能选择函数旋转数据处理函数
static void wifi_faction_handle1(void);    //wifi函数数据处理
static void server_faction_handle1(void);    //服务器
static void b_faction_handle1(void);    //蓝牙

static void encoder_isr_function0(char *arg);
static void encoder_isr_function1(char *arg);
//static void encoder_isr_function2(char *arg);

void encoder_init(void)
{
    GpioInit();
    IoSetFunc(WIFI_IOT_IO_NAME_GPIO_0, WIFI_IOT_IO_FUNC_GPIO_0_GPIO);
    GpioSetDir(WIFI_IOT_IO_NAME_GPIO_0, WIFI_IOT_GPIO_DIR_IN);
    IoSetPull(WIFI_IOT_IO_NAME_GPIO_0, WIFI_IOT_IO_PULL_UP);
    GpioRegisterIsrFunc(WIFI_IOT_IO_NAME_GPIO_0, WIFI_IOT_INT_TYPE_EDGE, WIFI_IOT_GPIO_EDGE_FALL_LEVEL_LOW, encoder_isr_function0, NULL);

    IoSetFunc(WIFI_IOT_IO_NAME_GPIO_1, WIFI_IOT_IO_FUNC_GPIO_1_GPIO);
    GpioSetDir(WIFI_IOT_IO_NAME_GPIO_1, WIFI_IOT_GPIO_DIR_IN);
    IoSetPull(WIFI_IOT_IO_NAME_GPIO_1, WIFI_IOT_IO_PULL_UP);
    GpioRegisterIsrFunc(WIFI_IOT_IO_NAME_GPIO_1, WIFI_IOT_INT_TYPE_EDGE, WIFI_IOT_GPIO_EDGE_FALL_LEVEL_LOW, encoder_isr_function1, NULL);

    IoSetFunc(WIFI_IOT_IO_NAME_GPIO_2, WIFI_IOT_IO_FUNC_GPIO_2_GPIO);
    GpioSetDir(WIFI_IOT_IO_NAME_GPIO_2, WIFI_IOT_GPIO_DIR_IN);
    IoSetPull(WIFI_IOT_IO_NAME_GPIO_2, WIFI_IOT_IO_PULL_UP);
}

//按下功能键
static void encoder_isr_function0(char *arg)
{
    (void) arg;
    int time_out = 0;
    WifiIotGpioValue gpio_value;
    osDelay(1); //延时10ms
    GpioGetInputVal(WIFI_IOT_IO_NAME_GPIO_0, &gpio_value);
    if(gpio_value == WIFI_IOT_GPIO_VALUE0)
    {
        while(gpio_value == WIFI_IOT_GPIO_VALUE0 && time_out <= 300)    //等待按键释放或者超时
        {
            GpioGetInputVal(WIFI_IOT_IO_NAME_GPIO_0, &gpio_value);
            time_out++;
            osDelay(1); //延时10ms
        }
        switch(oled_task_data.faction_num)
        {
            case SHOW_HOMEPAGE : main_faction_handle(); break;
            case SHOW_SELECT   : select_faction_handle0(); break;
            case SHOW_WIFI     : wifi_faction_handle0(); break;
            case SHOW_SERVER   : server_faction_handle0(); break;
            case SHOW_BLE      : b_faction_handle0(); break;
            case SHOW_CTRL     : break;
            case SHOW_SETTING  : break;
            case SHOW_BACK     : break;
            default            : break;
        }
        encoder_data.buzzer_on_flag = 1;
    }
}

	     

static void encoder_isr_function1(char *arg)
{
    (void) arg;
    int time_out = 0;
    osDelay(1); //延时10ms
    WifiIotGpioValue gpio_value_a;
    WifiIotGpioValue gpio_value_b;
    GpioGetInputVal(WIFI_IOT_IO_NAME_GPIO_1, &gpio_value_a);
    GpioGetInputVal(WIFI_IOT_IO_NAME_GPIO_2, &gpio_value_b);

    if(gpio_value_a == WIFI_IOT_GPIO_VALUE0 && gpio_value_b == WIFI_IOT_GPIO_VALUE1) //A脚高电平时，B脚为高电平，顺时针旋转；A脚先变为低电平，此时B脚还为高电平。
    {
        while(gpio_value_a == WIFI_IOT_GPIO_VALUE0 && time_out <= 300)     //超时时间为3s
        {
            GpioGetInputVal(WIFI_IOT_IO_NAME_GPIO_1, &gpio_value_a);
            time_out++;
            osDelay(1); //延时10ms
        }
        encoder_data.right_flag = 1;
        switch(oled_task_data.faction_num)
        {
            case 1 : select_faction_handle1(); break;
            case 2 : wifi_faction_handle1(); break;
            case 3 : server_faction_handle1(); break;
            case 4 : b_faction_handle1(); break;
            default : break;
        }
        encoder_data.buzzer_on_flag = 1;
    }
    else if(gpio_value_a == WIFI_IOT_GPIO_VALUE0 && gpio_value_b == WIFI_IOT_GPIO_VALUE0)    //A脚低电平时，B脚为高电平，逆时针旋转；B脚先变为低电平，此时AA脚还为高电平。
    {
        while(gpio_value_a == WIFI_IOT_GPIO_VALUE0 && time_out <= 300)
        {
            GpioGetInputVal(WIFI_IOT_IO_NAME_GPIO_1, &gpio_value_a);
            time_out++;
            osDelay(1); //延时10ms
        }
        encoder_data.left_flag = 1;
        switch(oled_task_data.faction_num)
        {
            case 1 : select_faction_handle1(); break;
            case 2 : wifi_faction_handle1(); break;
            case 3 : server_faction_handle1(); break;
            case 4 : b_faction_handle1(); break;
            case 6 : break;
            case 7 : break;
            default : break;
        }
        encoder_data.buzzer_on_flag = 1;
    }
}

static void main_faction_handle(void)
{
    oled_task_data.faction_num = 1;    //功能函数标识数改为功能选择函数//只有从主界面按下按键才会进入功能选择界面，那从其他返回?????
    oled_task_data.select_faction_state = SELECT;
    oled_task_data.select_faction_num = 0;
    oled_task_data.last_select_faction_num = -1;
}

static void select_faction_handle0(void)
{
    oled_task_data.select_faction_state = RUN;
}

static void wifi_faction_handle0(void)
{
    if(oled_task_data.wifi_faction_state == SELECT)
    {
        oled_task_data.wifi_faction_state = RUN;
    }
}

static void server_faction_handle0(void)
{
    if(oled_task_data.server_faction_state == SELECT)
    {
        oled_task_data.server_faction_state = RUN;
    }
}
static void b_faction_handle0(void)
{
    if(oled_task_data.b_faction_state == SELECT)
    {
        oled_task_data.b_faction_state = RUN;
    }
}

/*************************************************
 *    编码器旋转数据处理
 * ***********************************************/
static void select_faction_handle1(void)
{
    //oled_task_data.last_select_faction_num = oled_task_data.select_faction_num;    //记录上一次的选择
    if(encoder_data.right_flag == 1)
    {
        oled_task_data.select_faction_num++;
        if(oled_task_data.select_faction_num == 1)
        {
            oled_task_data.select_faction_num++;
        }
        else if(oled_task_data.select_faction_num >= 8)
        {
            oled_task_data.select_faction_num = 0;
        }
        encoder_data.right_flag = 0;
    }
    else if(encoder_data.left_flag == 1)
    {
        oled_task_data.select_faction_num--;
        if(oled_task_data.select_faction_num == 1)
        {
            oled_task_data.select_faction_num--;
        }
        else if(oled_task_data.select_faction_num <= -1)
        {
            oled_task_data.select_faction_num = 7;
        }
        encoder_data.left_flag = 0;
    }
}


static void wifi_faction_handle1(void)
{
    if(encoder_data.right_flag == 1)
    {
        oled_task_data.wifi_faction_num++;
        if(oled_task_data.wifi_faction_num == 4)
        {
            oled_task_data.wifi_faction_num = 1;
        }
        encoder_data.right_flag = 0;
    }
    else if(encoder_data.left_flag == 1)
    {
        oled_task_data.wifi_faction_num--;
        if(oled_task_data.wifi_faction_num == -1)
        {
            oled_task_data.wifi_faction_num = 3;
        }
        encoder_data.left_flag = 0;
    }
}

static void server_faction_handle1(void)
{
    if(encoder_data.right_flag == 1)
    {
        oled_task_data.server_faction_num++;
        if(oled_task_data.server_faction_num == 4)
        {
            oled_task_data.server_faction_num = 1;
        }
        encoder_data.right_flag = 0;
    }
    else if(encoder_data.left_flag == 1)
    {
        oled_task_data.server_faction_num--;
        if(oled_task_data.server_faction_num == -1)
        {
            oled_task_data.server_faction_num = 3;
        }
        encoder_data.left_flag = 0;
    }
}

static void b_faction_handle1(void)
{
    if(encoder_data.right_flag == 1)
    {
        oled_task_data.b_faction_num++;
        if(oled_task_data.b_faction_num == 4)
        {
            oled_task_data.b_faction_num = 1;
        }
        encoder_data.right_flag = 0;
    }
    else if(encoder_data.left_flag == 1)
    {
        oled_task_data.b_faction_num--;
        if(oled_task_data.b_faction_num == -1)
        {
            oled_task_data.b_faction_num = 3;
        }
        encoder_data.left_flag = 0;
    }
}